package com.jun.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jun.pojo.entity.Shop;
import com.jun.pojo.vo.R;
import com.jun.service.FoodService;
import com.jun.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {

  @Autowired
  private ShopService shopService;

  @Autowired
  private FoodService foodService;

  @GetMapping(path = {"/{foodId}/{userId}", "/{foodId}"})
  public R getShopList(@PathVariable("foodId") Integer foodId,
                       @PathVariable(value = "userId", required = false) Integer userId) {
    Map<String, Object> result = new HashMap<>(2);

    // 查询shop列表
    QueryWrapper<Shop> qw = new QueryWrapper<>();
    qw.eq("food_id", foodId);
    List<Shop> list = shopService.list(qw);

    // 将shop添加到map
    result.put("list", list);

   //用户是否登录
    if (userId == null){
      // 用户未登录
      result.put("isLove", false);
    } else {
      // 用户登录
      boolean flag = foodService.hasLove(foodId, userId);
      result.put("isLove", flag);
    }

    return new R(result);
  }

}
